package com.bus.admin.serviceImpl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.bus.admin.bean.Orderbus;
import com.bus.admin.bean.PayRequest;
import com.bus.admin.service.AliPayService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @项目名称: bus-admin
 * @包名称: com.bus.admin.serviceImpl
 * @创建人员: Mr.Administrator  张子艺
 * @创建时间: 2020-04-20 09:48
 * @版本:
 */

@Service
public class AliPayServiceImpl implements AliPayService {

    @Value("${ali.pay.APP_ID}")
    String APP_ID;
    @Value("${ali.pay.APP_PRIVATE_KEY}")
    String APP_PRIVATE_KEY;
    @Value("${ali.pay.CHARSET}")
    String CHARSET;
    @Value("${ali.pay.ALIPAY_PUBLIC_KEY}")
    String ALIPAY_PUBLIC_KEY;
    @Value("${ali.pay.RETURN_URL}")
    String RETURN_URL;
    @Value("${ali.pay.is_prod}")
    String is_prod;

   private  AlipayClient getClient(){
        AlipayClient alipayClient=null;
        if(is_prod.equals("1")){
            alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
        }else{
            alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
        }
        return alipayClient;
    }

    @Override
    public String  createOrder(String moeny) {
//实例化客户端
        AlipayClient alipayClient = getClient();
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody("请支付");
        model.setSubject("支付");
        model.setOutTradeNo(RandomUtil.randomNumbers(30));
        model.setTimeoutExpress("30m");
        model.setTotalAmount(moeny);
        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setBizModel(model);
        request.setNotifyUrl(RETURN_URL);
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            System.out.println(response.getBody());
            return response.getBody();//就是orderString 可以直接给客户端请求，无需再做处理。
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String createH5Order(PayRequest payRequest, Orderbus orderbus) {
        AlipayClient alipayClient = getClient();
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();
        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setSubject(payRequest.getOrdername());
        model.setBody(payRequest.getOrdername());
        Map map=new HashMap();
        if(payRequest.getAttach().equals("定金")){
            model.setTotalAmount(orderbus.getDingjin());
            map.put("payType","定金");
            model.setPassbackParams(JSONObject.toJSONString(map));
        }
        if(payRequest.getAttach().equals("尾款")){
            map.put("payType","尾款");
            model.setPassbackParams(JSONObject.toJSONString(map));
            model.setTotalAmount(orderbus.getWeikuan());
        }
        if(payRequest.getAttach().equals("全款")){
            map.put("payType","全款");
            model.setPassbackParams(JSONObject.toJSONString(map));
            model.setTotalAmount(orderbus.getJine());
        }
        model.setOutTradeNo(payRequest.getOrdernum());
        model.setTimeoutExpress("30m");
        model.setProductCode("QUICK_MSECURITY_PAY");

        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(RETURN_URL);
        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = alipayClient.pageExecute(alipay_request).getBody();
            System.out.println(form);
            return form;
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


}
